import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:my_flutter/core/Routers.dart';

import 'flutter_params.dart';
import 'h5_params.dart';


///webview传过来的回调函数
late Function(String result) _callJWebView;

///flutter -> h5 ( 回调js的方法)
callJs(FlutterParams flutterParams) {
  String str = JsonEncoder().convert(flutterParams);
  _callJWebView(str);
}

//获取H5Params的参数
dynamic getParamByName(H5Params h5Params, String name) {
  //对象 -> string -> map
  Object? params = h5Params.params;
  String str = JsonEncoder().convert(params);
  Map<dynamic, dynamic> map = JsonDecoder().convert(str);
  return map[name];
}

///h5 -> flutter (回调flutter的方法)
callFlutter(H5Params h5Params, Function(String result) callJWebView) {

  //初始化_callJWebView
  _callJWebView = callJWebView;
  //取参数
  String? jsCallBackId = h5Params.jsCallBackId;
  String action = h5Params.action;
  switch (action) {
    case "getUserInfo":
      getUserInfo(jsCallBackId!);
      break;
    case "getDeviceInfo": //获取设备信息
      getDeviceInfo(jsCallBackId!);
      break;
    case "nativeScan": // 扫描
      nativeScan(h5Params);
      break;
    case "closeWebView": // 关闭webview
      closeWebView(h5Params);
      break;
    case "openPhoneBook": // 打开通讯录，选择联系人
      openPhoneBook(h5Params);
      break;
    case "callPhone": // 打电话
      callPhone(h5Params);
      break;
  }
}


getUserInfo(String jsCallBackId) {
  //这里可以获取你自己真实的用户数据
  var person= {"name": "张三丰", "age": 100, "phone": "17717572318"};
  FlutterParams flutterParams = FlutterParams();
  flutterParams.jsCallBackId = jsCallBackId;
  flutterParams.data = person;
  callJs(flutterParams);
}

void getDeviceInfo(String jsCallBackId) async {
  //获取你真实的设备信息
  var deviceInfo = {"deviceName": "红米note11", "systemVersion": "Android-12", "UUID": "XXXXXXXXX11111111"};
  FlutterParams flutterParams = FlutterParams();
  flutterParams.jsCallBackId = jsCallBackId;
  flutterParams.data = deviceInfo;
  callJs(flutterParams);
}

void closeWebView(H5Params h5params) {
  Routers.pop();
}

void callPhone(H5Params h5Params) {
  String phoneNum = getParamByName(h5Params, "phoneNum");
  //调用UrlLauncher，打电话
  // UrlLauncher.tel(phoneNum);
}

void openPhoneBook(H5Params h5params) async {
//  final ContactPicker _contactPicker = new ContactPicker();
//  Contact contact = await _contactPicker.selectContact();
//
//  FlutterContact flutterContact = FlutterContact();
//  flutterContact.phone = contact.phoneNumber.number;
//  flutterContact.name = contact.fullName;
//
//  print(flutterContact.toJson());
//
//  FlutterParams params = FlutterParams();
//  params.data = flutterContact;
//  params.jsCallBackId = h5params.jsCallBackId;
//  callJs(params);
}

void nativeScan(H5Params h5Params) {

  FlutterParams flutterParams = FlutterParams();
  //跳转到扫描界面
  // Future res = Routers.push(RouteName.scan_page);

  //获取到扫描界面的结果，回调给h5
  // res.then((msg) => {
  //       flutterParams.jsCallBackId = h5Params.jsCallBackId,
  //       flutterParams.data = msg,
  //       callJs(flutterParams),
  //     });
}
